package com.star.bookstore.product.controller;

import com.star.bookstore.common.utils.R;
import com.star.bookstore.product.feign.ProductFeignService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.star.bookstore.product.interceptor.LoginInterceptor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RefreshScope
@RestController
public class IndexController {

    @Autowired
    ProductFeignService productFeignService;

    /*//测试远程调用
    @RequestMapping(value = {"/","/login"})
    public R goIndex(){
        System.out.println("开始调用");
        R r = productFeignService.getLoginStatus("1","sdfasd");
        System.out.println("调用结果：" + r.get("loginStatus"));
        return R.ok().put("loginStatus",r.get("loginStatus"));
    }*/

    //检查是否拦截
    @GetMapping("/product/login/check")
    public R loginCheck(HttpServletRequest request, HttpServletResponse response){

        String loginNumber = request.getParameter("loginNumber");
        String userId = request.getParameter("userId");

        System.out.println("调用的参数:userId="+userId+" loginNumber= " +loginNumber);
        //TODO 需要注意的点：前端穿过来的null实为"null"字符串
        if ("null".equals(userId) || !StringUtils.isNumeric(userId) || "null".equals(loginNumber)){
            return R.error().put("msg","未登录");
        }
        //远程调用用户服务以获取对应的用户对象的登录码
        R r = productFeignService.getLoginStatus(userId,loginNumber);
        Boolean loginStatus = (Boolean) r.get("loginStatus");
        System.out.println("状态：" + loginNumber);
        if (!loginStatus){
            return R.error().put("msg","未登录");
        }else{
            request.getSession().setAttribute("token2",userId);
            return R.ok().put("msg","已登录");
        }
    }


}
